package com.net.xpay.admin.controller.admin;

import com.google.common.base.Preconditions;
import com.net.common.model.ResultData;
import com.net.xpay.admin.vo.UserRegistrationVo;
import com.net.xpay.common.domain.UserRegistration;
import com.net.xpay.common.manager.UserRegistrationManager;
import com.net.xpay.common.model.UserRegistrationModel;
import com.net.xpay.common.service.UserRegistrationService;
import com.net.xpay.core.model.IdModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


@RestController
@Slf4j
@RequestMapping("/admin/user-registration")
public class AdminUserRegistrationController {

    @Autowired
    private UserRegistrationService userRegistrationService;

    @Autowired
    private UserRegistrationManager userRegistrationManager;

    /**
     * 用户备案
     */
    @ApiOperation("用户备案")
    @PostMapping("/submit")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'user-registration::submit')")
    public ResultData<Boolean> submitUserRegistration(@RequestBody UserRegistrationModel model)  {
        Preconditions.checkArgument(model != null && model.getUserId() != null);
        userRegistrationService.submitUserRegistration(model);
        return new ResultData<>(true);
    }

    /**
     * 用户备案
     */
    @ApiOperation("用户备案根据用户id查询")
    @PostMapping("/get-by-user-id")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'user-registration::get-by-user-id')")
    public ResultData<UserRegistrationVo> getByUserId(@RequestBody IdModel model)  {
        Preconditions.checkArgument(model != null );
        UserRegistration userRegistration = userRegistrationManager.getByUserId(model.getId());
        UserRegistrationVo vo = new UserRegistrationVo();
        if (userRegistration != null){
            vo.setBusinessLicense(userRegistration.getBusinessLicense());
            vo.setIdCardFront(userRegistration.getIdCardFront());
            vo.setIdCardBack(userRegistration.getIdCardBack());
            vo.setBizSceneDesc(userRegistration.getBizSceneDesc());
            vo.setMerchantCommitment(userRegistration.getMerchantCommitment());
            vo.setServiceRatio(userRegistration.getServiceRatio());
        }
        return new ResultData<>(vo);

    }
}
